package zj.com.fragments;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.xutils.DbManager;

import zj.com.activities.DjcListActivity;
import zj.com.activities.DqrListActivity;
import zj.com.activities.DshListActivity;
import zj.com.activities.DzgListActivity;
import zj.com.activities.GstjActivity;
import zj.com.activities.JcdEditActivity;
import zj.com.activities.ProjecttjActivity;
import zj.com.app.ZjApplication;
import zj.com.db.DBUtil;
import zj.com.model.Privilege;
import zj.com.net.Urls;
import zj.com.util.Constants;
import zj.com.util.ImageUtil;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/3.
 */
//现场检查
public class XcjcFragment  extends Fragment implements View.OnClickListener{


    private TextView title_name;
    private TextView right_tv;
    private RelativeLayout djc_rl,dqr_rl,dzg_rl,dsh_rl,xm_rl,gs_rl;
    public Intent intent;

    public ImageView banner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        View view=  inflater.inflate(R.layout.fragment_xcjc, container, false);
        initView(view);
        return view;
    }

    public Privilege djc;
    public Privilege dqr;
    public Privilege dzg;
    public Privilege dsh;
    public Privilege gstj;
    public Privilege xmtj;
    public void initView(View view){



        DbManager dbManager =  DBUtil.getInstance(getActivity()).getDBManager();
        try{
            djc = dbManager.selector(Privilege.class).where("name","=", Constants.ToBeInspectedInspection).findFirst();
            dqr = dbManager.selector(Privilege.class).where("name","=", Constants.ToBeConfirmedInspection).findFirst();
            dzg = dbManager.selector(Privilege.class).where("name","=", Constants.ToBeRepairedInspection).findFirst();
            dsh = dbManager.selector(Privilege.class).where("name","=", Constants.ToBeAuditedInspection).findFirst();
            gstj = dbManager.selector(Privilege.class).where("name","=", Constants.CompanyAnalysis).findFirst();
            xmtj = dbManager.selector(Privilege.class).where("name","=", Constants.ProjectAnalysis).findFirst();

        }catch (Exception e){

        }
        banner = (ImageView) view.findViewById(R.id.banner);

        String url =  String.format(Urls.GETAdv,"SQCP_App_security_header");
        LogUtils.getInstance().error("url========="+url);

        DisplayImageOptions options = ImageUtil.getOption();
        ImageLoader.getInstance().displayImage(url,banner,options);

        title_name = (TextView) view.findViewById(R.id.title_name);
        title_name.setText("现场检查");
        right_tv = (TextView) view.findViewById(R.id.right_tv);
        right_tv.setText("创建");
        right_tv.setVisibility(View.GONE);
        right_tv.setOnClickListener(this);

        djc_rl = (RelativeLayout) view.findViewById(R.id.djc_rl);
        djc_rl.setOnClickListener(this);
        dqr_rl = (RelativeLayout) view.findViewById(R.id.dqr_rl);
        dqr_rl.setOnClickListener(this);
        dzg_rl = (RelativeLayout) view.findViewById(R.id.dzg_rl);
        dzg_rl.setOnClickListener(this);
        dsh_rl = (RelativeLayout) view.findViewById(R.id.dsh_rl);
        dsh_rl.setOnClickListener(this);
        xm_rl = (RelativeLayout) view.findViewById(R.id.xm_rl);
        xm_rl.setOnClickListener(this);
        gs_rl = (RelativeLayout) view.findViewById(R.id.gs_rl);
        gs_rl.setOnClickListener(this);

        if(djc == null){
            djc_rl.setBackgroundResource(R.drawable.round_color_disable);
        }
        if(dzg == null){
            dzg_rl.setBackgroundResource(R.drawable.round_color_disable);
        }
        if(dqr == null){
            dqr_rl.setBackgroundResource(R.drawable.round_color_disable);

        }

        if(dsh == null){
            dsh_rl.setBackgroundResource(R.drawable.round_color_disable);
        }
        if(dqr == null){
            dqr_rl.setBackgroundResource(R.drawable.round_color_disable);
        }
        if(gstj == null){
            gs_rl.setBackgroundResource(R.drawable.round_color_disable);
        }
        if(xmtj == null){
            xm_rl.setBackgroundResource(R.drawable.round_color_disable);
        }
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }



    @Override
    public void onClick(View v) {

        switch(v.getId()){
            case R.id.right_tv:
                ZjApplication.pgScan = null;
                ZjApplication.dqrJcdInfo = null;
                ZjApplication.jcd = null;
                ZjApplication.inspectItems.clear();
                ZjApplication.isUpdateJcd = false;
                intent = new Intent(getActivity(), JcdEditActivity.class);
                intent.putExtra("mode",1);
                startActivity(intent);
                break;
            case R.id.djc_rl:
                if(djc == null){

                    Toast.makeText(getActivity(),"你没有此模块权限",Toast.LENGTH_SHORT).show();
                    return;
                }
                intent = new Intent(getActivity(), DjcListActivity.class);
                startActivity(intent);
                break;
            case R.id.dqr_rl:
                if(dqr == null){

                    Toast.makeText(getActivity(),"你没有此模块权限",Toast.LENGTH_SHORT).show();
                    return;
                }
                intent = new Intent(getActivity(),  DqrListActivity.class);
                startActivity(intent);
                break;
            case R.id.dzg_rl:
                if(dzg == null){

                    Toast.makeText(getActivity(),"你没有此模块权限",Toast.LENGTH_SHORT).show();
                    return;
                }
                intent = new Intent(getActivity(),  DzgListActivity.class);
                startActivity(intent);
                break;
            case R.id.dsh_rl:
                if(dsh == null){

                    Toast.makeText(getActivity(),"你没有此模块权限",Toast.LENGTH_SHORT).show();
                    return;
                }
                intent = new Intent(getActivity(),  DshListActivity.class);
                startActivity(intent);
                break;
            case R.id.xm_rl:
                if(xmtj == null){

                    Toast.makeText(getActivity(),"你没有此模块权限",Toast.LENGTH_SHORT).show();
                    return;
                }
                intent = new Intent(getActivity(),  ProjecttjActivity.class);
                startActivity(intent);
                break;
            case R.id.gs_rl:
                if(gstj == null){
                    Toast.makeText(getActivity(),"你没有此模块权限",Toast.LENGTH_SHORT).show();
                    return;
                }
                intent = new Intent(getActivity(),  GstjActivity.class);
                startActivity(intent);
                break;

        }

    }
}
